{%- set mpi_radio_vendor_name = sysbuild['config']['SB_CONFIG_SUIT_MPI_RAD_LOCAL_1_VENDOR_NAME']|default('nordicsemi.com') %}
{%- set mpi_radio_class_name = sysbuild['config']['SB_CONFIG_SUIT_MPI_RAD_LOCAL_1_CLASS_NAME']|default('nRF54H20_sample_rad') %}
{%- set suit_artifacts_base_dir = ( radio['binary'].split('/')[:-1] | join('/') ) %}
{%- if 'CONFIG_SUIT_ENVELOPE_TARGET_ENCRYPT' in radio['config'] and radio['config']['CONFIG_SUIT_ENVELOPE_TARGET_ENCRYPT'] != '' %}
  {%- set encrypted = True %}
  {%- set rad_encryption_dir = ( suit_artifacts_base_dir ) + "/" + ( radio['name'] ) + "_encryption_artifacts" %}
{%- else %}
  {%- set encrypted = False %}
{%- endif %}
SUIT_Envelope_Tagged:
  suit-authentication-wrapper:
    SuitDigest:
      suit-digest-algorithm-id: cose-alg-sha-256
  suit-manifest:
    suit-manifest-version: 1
{%- if RAD_LOCAL_1_SEQ_NUM is defined %}
    suit-manifest-sequence-number: {{ RAD_LOCAL_1_SEQ_NUM }}
{%- elif DEFAULT_SEQ_NUM is defined %}
    suit-manifest-sequence-number: {{ DEFAULT_SEQ_NUM }}
{%- else %}
    suit-manifest-sequence-number: 1
{%- endif %}
    suit-common:
      suit-components:
      - - MEM
        - {{ radio['dt'].label2node['cpu'].unit_addr }}
        - {{ get_absolute_address(radio['dt'].chosen_nodes['zephyr,code-partition']) }}
        - {{ radio['dt'].chosen_nodes['zephyr,code-partition'].regs[0].size }}
      - - CAND_IMG
        - 0
      - - CACHE_POOL
        - 1
      suit-shared-sequence:
      - suit-directive-set-component-index: 0
      - suit-directive-override-parameters:
          suit-parameter-vendor-identifier:
            RFC4122_UUID: {{ mpi_radio_vendor_name }}
          suit-parameter-class-identifier:
            RFC4122_UUID:
              namespace: {{ mpi_radio_vendor_name }}
              name: {{ mpi_radio_class_name }}
          suit-parameter-image-digest:
            suit-digest-algorithm-id: cose-alg-sha-256
            suit-digest-bytes:
{%- if encrypted %}
              file_direct: {{ rad_encryption_dir }}/plain_text_digest.bin
{%- else %}
              file: {{ radio['binary'] }}
{%- endif %}
          suit-parameter-image-size:
{%- if encrypted %}
            file_direct: {{ rad_encryption_dir }}/plain_text_size.txt
{%- else %}
            file: {{ radio['binary'] }}
{%- endif %}
      - suit-condition-vendor-identifier:
        - suit-send-record-success
        - suit-send-record-failure
        - suit-send-sysinfo-success
        - suit-send-sysinfo-failure
      - suit-condition-class-identifier:
        - suit-send-record-success
        - suit-send-record-failure
        - suit-send-sysinfo-success
        - suit-send-sysinfo-failure
    suit-validate:
    - suit-directive-set-component-index: 0
    # In the case of streaming operations it is worth to retry them at least once.
    # This increases the robustness against bit flips on the transport,
    # for example when storing the data on an external memory device.
    # The suit-directive-try-each will complete on the first successful subsequence.
    - suit-directive-try-each:
      - - suit-condition-image-match:
          - suit-send-record-success
          - suit-send-record-failure
          - suit-send-sysinfo-success
          - suit-send-sysinfo-failure
      - - suit-condition-image-match:
          - suit-send-record-success
          - suit-send-record-failure
          - suit-send-sysinfo-success
          - suit-send-sysinfo-failure
    suit-invoke:
    - suit-directive-set-component-index: 0
    - suit-directive-invoke:
      - suit-send-record-failure

{%- if RAD_LOCAL_1_VERSION is defined %}
    suit-current-version: {{ RAD_LOCAL_1_VERSION }}
{%- elif DEFAULT_VERSION is defined %}
    suit-current-version: {{ DEFAULT_VERSION }}
{%- endif %}

{%- if 'CONFIG_SUIT_DFU_CACHE_EXTRACT_IMAGE' not in radio['config'] or radio['config']['CONFIG_SUIT_DFU_CACHE_EXTRACT_IMAGE'] == '' %}
    suit-payload-fetch:
    - suit-directive-set-component-index: 2
    - suit-directive-override-parameters:
        suit-parameter-uri: "file://{{ radio['filename'] }}"
    - suit-directive-fetch:
      - suit-send-record-failure
{%- endif %}

    suit-install:
    - suit-directive-set-component-index: 1
    - suit-directive-override-parameters:
{%- if 'CONFIG_SUIT_DFU_CACHE_EXTRACT_IMAGE_URI' in radio['config'] and radio['config']['CONFIG_SUIT_DFU_CACHE_EXTRACT_IMAGE_URI'] != '' %}
        suit-parameter-uri: "{{ radio['config']['CONFIG_SUIT_DFU_CACHE_EXTRACT_IMAGE_URI'] }}"
{%- else %}
        suit-parameter-uri: "file://{{ radio['filename'] }}"
{%- endif %}
        suit-parameter-image-digest:
          suit-digest-algorithm-id: cose-alg-sha-256
          suit-digest-bytes:
            file: {{ radio['binary'] }}
    - suit-directive-fetch:
      - suit-send-record-failure
    - suit-directive-try-each:
      - - suit-condition-image-match:
          - suit-send-record-success
          - suit-send-record-failure
          - suit-send-sysinfo-success
          - suit-send-sysinfo-failure
      - - suit-condition-image-match:
          - suit-send-record-success
          - suit-send-record-failure
          - suit-send-sysinfo-success
          - suit-send-sysinfo-failure
    - suit-directive-set-component-index: 0
    - suit-directive-override-parameters:
        suit-parameter-source-component: 1
{%- if encrypted %}
        suit-parameter-encryption-info:
          file: {{ rad_encryption_dir }}/suit_encryption_info.bin
{%- endif %}
    # When copying the data it is worth to retry the sequence of
    # suit-directive-copy and suit-condition-image-match at least once.
    # If a bit flip occurs, it might be due to a transport issue, not
    # a corrupted candidate image. In this case the bit flip is recoverable
    # and it is worth retrying the operation.
    # The suit-directive-try-each will complete on the first successful subsequence.
    - suit-directive-try-each:
      - - suit-directive-copy:
          - suit-send-record-failure
        - suit-condition-image-match:
          - suit-send-record-success
          - suit-send-record-failure
          - suit-send-sysinfo-success
          - suit-send-sysinfo-failure
      - - suit-directive-copy:
          - suit-send-record-failure
        - suit-condition-image-match:
          - suit-send-record-success
          - suit-send-record-failure
          - suit-send-sysinfo-success
          - suit-send-sysinfo-failure
    suit-text:
      suit-digest-algorithm-id: cose-alg-sha-256

    suit-candidate-verification:
    - suit-directive-set-component-index: 1
    - suit-directive-override-parameters:
{%- if 'CONFIG_SUIT_DFU_CACHE_EXTRACT_IMAGE_URI' in radio['config'] and radio['config']['CONFIG_SUIT_DFU_CACHE_EXTRACT_IMAGE_URI'] != '' %}
        suit-parameter-uri: "{{ radio['config']['CONFIG_SUIT_DFU_CACHE_EXTRACT_IMAGE_URI'] }}"
{%- else %}
        suit-parameter-uri: "file://{{ radio['filename'] }}"
{%- endif %}
        suit-parameter-image-digest:
          suit-digest-algorithm-id: cose-alg-sha-256
          suit-digest-bytes:
            file: {{ radio['binary'] }}
{%- if encrypted %}
    # For the encrypted image this fetch directive is used to verify the tag and the AAD
    # of the received encrypted image The target "CAND_IMG" behaves like a /dev/null device
    # and all the data is discarded.
    # This way even if the encrypted content is incorrect, the contents of the target memory
    # will not be affected.
    # Note that no digest checking is required on the encrypted content itself, as checking the tag
    # and the AAD verifies the integrity of the content. In fact, suit-condition-image-match
    # won't be able to work in this case, as the CAND_IMG won't contain any valid content.
          suit-parameter-encryption-info:
            file: {{ rad_encryption_dir }}/suit_encryption_info.bin
{%- endif %}
    - suit-directive-fetch:
      - suit-send-record-failure
{%- if not encrypted %}
    - suit-directive-try-each:
      - - suit-condition-image-match:
          - suit-send-record-success
          - suit-send-record-failure
          - suit-send-sysinfo-success
          - suit-send-sysinfo-failure
      - - suit-condition-image-match:
          - suit-send-record-success
          - suit-send-record-failure
          - suit-send-sysinfo-success
          - suit-send-sysinfo-failure
{%- endif %}
    # declare radio image MRAM memory as IPUC
    - suit-directive-set-component-index: 0
    - suit-directive-override-parameters:
        suit-parameter-image-size:
          raw: 0

    suit-manifest-component-id:
    - INSTLD_MFST
    - RFC4122_UUID:
        namespace: {{ mpi_radio_vendor_name }}
        name: {{ mpi_radio_class_name }}
  suit-text:
    en:
      '["MEM", {{ radio['dt'].label2node['cpu'].unit_addr }}, {{ get_absolute_address(radio['dt'].chosen_nodes['zephyr,code-partition']) }}, {{ radio['dt'].chosen_nodes['zephyr,code-partition'].regs[0].size }}]':
        suit-text-vendor-name: Nordic Semiconductor ASA
        suit-text-model-name: nRF54H20_cpurad
        suit-text-vendor-domain: nordicsemi.com
        suit-text-model-info: The nRF54H20 radio core
        suit-text-component-description: Sample radio core FW
        suit-text-component-version: v1.0.0
  suit-integrated-payloads: {}
